package org.yxj.videolibrary.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import org.yxj.videolibrary.i.PlayVideoListener;
import org.yxj.videolibrary.view.VideoLoadView;
import org.yxj.videolibrary.view.YXJVideoView;

import java.io.IOException;

/**
 * 单利视频播放管理类
 *
 * @author on 2022/9/12
 */
public class YXJVideoManager {

    private static YXJVideoManager manager;

    private YXJVideoView yxjVideoView;

    private Activity context;

    /**
     * 正常情况下的宿主view
     */
    private FrameLayout container;

    private FrameLayout tinyFrameLayout;

    /**
     * 进入全屏的时候支持添加子view
     */
    private View childView;
    private FrameLayout.LayoutParams childParams;
    private Rect marginLayoutChildParams;


    public static final int Normal_Type = 0;
    public static final int Full_Type = 1;
    public static final int Tiny_Type = 2;
    public int currentType = Normal_Type;

//    public static YXJVideoManager getInstance() {
//        if (null == manager) {
//            synchronized (YXJVideoManager.class) {
//                if (null == manager) {
//                    manager = new YXJVideoManager();
//                }
//            }
//        }
//        return manager;
//    }


    /**
     * 初始化
     *
     * @param context
     */
    public void init(Activity context, FrameLayout container) {
        onDestroy();
        this.context = context;
        this.container = container;
        initConfig();
    }


    private void initConfig() {
        yxjVideoView = new YXJVideoView(context);
        yxjVideoView
                .setContainerView(container)
                .setAutoPlay(true)
                .setSupportControl(false)
                .setSupportOnLongClick(false)
                .setRepeatPlay(true)
                .setSupportOnClick(true)
                .setSupportHeadView(false)
                .setSupportSubmergedBar(false)
                .setPlayVideoListener(playVideoListener)
                .setDefaultLoad(VideoLoadView.LoadAnimMode.FRAME_ANIM);
        container.addView(yxjVideoView);
    }



    public YXJVideoView getYxjVideoView() {
        return yxjVideoView;
    }

    /**
     * @param videoUrl
     * @param imgUrl
     */
    public void setData(String videoUrl, String imgUrl) {
        try {
            if (yxjVideoView != null)
                yxjVideoView.setPlaceHolder(imgUrl).setDataSource(videoUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private PlayVideoListener playVideoListener = new PlayVideoListener() {

        @Override
        public void onHeadBack() {
            if (videoListener != null) {
                videoListener.onHeadBack();
            }
            normalScreen();
        }


        @Override
        public void onLongClick() {
            if (videoListener != null) {
                videoListener.onLongClick();
            }
        }


        @Override
        public void onClickVideo() {

            if (videoListener != null) {
                videoListener.onClickVideo();
            }

            switch (currentType){
                case Full_Type:
                    //全屏模式下
                    if (!yxjVideoView.isPlaying()) {
                        start();
                    } else {
                        onPause();
                    }
                    break;

                case Normal_Type:
                    if (!yxjVideoView.isPlaying()){
                        start();
                    }else {
                        fullScreen();
                    }
                    break;

                default:{
                    fullScreen();
                    break;
                }
            }
//            if (currentType == Full_Type) {
//                //全屏模式下
//                if (!yxjVideoView.isPlaying()) {
//                    start();
//                } else {
//                    onPause();
//                }
//            } else {
//                fullScreen();
//            }
        }
    };


    private PlayVideoListener videoListener;

    public void setVideoListener(PlayVideoListener videoListener) {
        this.videoListener = videoListener;
    }


    /**
     * 进入全屏的时候支持添加子view
     */
    public YXJVideoManager addChildView(View view, FrameLayout.LayoutParams params, Rect marginLayoutParams) {
        childView = view;
        childParams = params;
        marginLayoutChildParams = marginLayoutParams;
        return this;
    }


    /**
     * 退出全屏
     */
    public void normalScreen() {
        reset();
        if (container != null) {
            container.removeAllViews();
            FrameLayout.LayoutParams f = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            f.gravity = Gravity.CENTER;
            container.addView(yxjVideoView, f);
            yxjVideoView
                    .setSupportHeadView(false)
                    .setSupportControl(false)
                    .setSupportOnLongClick(false)
                    .setSupportOnClick(true);
//            yxjVideoView.setScreenSizeStyle(YXJVideoView.ScreenSizeStyle.heightFull);
//            yxjVideoView.showScreenSizeStyle();
            yxjVideoView.setAdjustTextureViewSize(container.getMeasuredWidth());
//            yxjVideoView.setPlaceHolderSize(container.getMeasuredWidth(),container.getMeasuredHeight());
        }
        currentType = Normal_Type;
        if (currentWindowStateListener != null)
            currentWindowStateListener.onChangerWindowState(currentType);
    }


    /**
     * 重置
     */
    private void reset() {

        if (yxjVideoView == null) return;

        if (container != null) {
            container.removeView(yxjVideoView);
        }


        FrameLayout frameLayout = context.findViewById(android.R.id.content);
        frameLayout.removeView(yxjVideoView);


        if (childView != null) {
            frameLayout.removeView(childView);
        }

        if (tinyFrameLayout != null) {
            tinyFrameLayout.removeAllViews();
            FrameLayout frameLayout1 = context.findViewById(android.R.id.content);
            frameLayout1.removeView(tinyFrameLayout);
        }
    }


    /**
     * 小窗口
     * <p>
     * 飘在右上角
     */
    public void tinyScreen(int topMargin) {

        reset();

        if (tinyFrameLayout == null) {
            tinyFrameLayout = new FrameLayout(context);
        }

        FrameLayout frameLayout1 = context.findViewById(android.R.id.content);
        FrameLayout.LayoutParams f = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        f.gravity = Gravity.RIGHT | Gravity.TOP;
        int w = context.getResources().getDisplayMetrics().widthPixels;
        f.width = w / 2;
        f.height = w / 3;
        frameLayout1.addView(tinyFrameLayout, f);


        RelativeLayout.MarginLayoutParams marginLayoutParams = new RelativeLayout.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        marginLayoutParams.leftMargin = 0;
        tinyFrameLayout.addView(yxjVideoView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        yxjVideoView
                .setSupportHeadView(true)
                .setSupportSubmergedBar(false)
                .setSupportControl(false)
                .setSupportOnLongClick(false)
                .setSupportOnClick(true)
                .setHeadCancelIcon(null, marginLayoutParams);

//        yxjVideoView.setAdjustTextureViewSize(w / 2);
        yxjVideoView.setAdjustTextureViewSize(w / 2,w / 3);
        yxjVideoView.setPlaceHolderSize(w / 2,w / 3);

        ViewGroup.MarginLayoutParams m = (ViewGroup.MarginLayoutParams) tinyFrameLayout.getLayoutParams();
        m.topMargin = topMargin;
        tinyFrameLayout.setLayoutParams(m);

        currentType = Tiny_Type;
        if (currentWindowStateListener != null)
            currentWindowStateListener.onChangerWindowState(currentType);
    }


    /**
     * 进入全频
     */
    public void fullScreen() {
        reset();

        FrameLayout frameLayout = context.findViewById(android.R.id.content);
        frameLayout.removeView(yxjVideoView);

        FrameLayout.LayoutParams f = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        f.gravity = Gravity.CENTER;
        frameLayout.addView(yxjVideoView, f);

        RelativeLayout.MarginLayoutParams marginLayoutParams = new RelativeLayout.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        marginLayoutParams.leftMargin = 30;
        yxjVideoView.setSupportHeadView(true)
                .setSupportSubmergedBar(true)
                .setSupportControl(true)
                .setSupportOnLongClick(true)
                .setSupportOnClick(true)
                .setHeadCancelIcon(null, marginLayoutParams);
        yxjVideoView.setScreenSizeStyle(YXJVideoView.ScreenSizeStyle.widthFull);
        yxjVideoView.showScreenSizeStyle();
        /*添加子view*/
        addChildViews(frameLayout);


        currentType = Full_Type;

        if (currentWindowStateListener != null)
            currentWindowStateListener.onChangerWindowState(currentType);
    }


    /**
     * 执行添加view
     *
     * @param frameLayout
     */
    private void addChildViews(FrameLayout frameLayout) {
        if (childView != null) {

            if (childParams != null) {
                frameLayout.addView(childView, childParams);
            } else {
                frameLayout.addView(childView);
            }

            if (marginLayoutChildParams != null) {
                ViewGroup.MarginLayoutParams margin = (ViewGroup.MarginLayoutParams) childView.getLayoutParams();
                if (marginLayoutChildParams.left != 0) {
                    margin.leftMargin = marginLayoutChildParams.left;
                }
                if (marginLayoutChildParams.right != 1) {
                    margin.rightMargin = marginLayoutChildParams.right;
                }
                if (marginLayoutChildParams.top != 0) {
                    margin.topMargin = marginLayoutChildParams.top;
                }
                if (marginLayoutChildParams.bottom != 0) {
                    margin.bottomMargin = marginLayoutChildParams.bottom;
                }

                childView.setLayoutParams(margin);
            }
        }
    }


    /**
     * 播放
     */
    public void start() {
        if (yxjVideoView != null)
            yxjVideoView.start();
    }


    /**
     * 暂停
     */
    public void onPause() {
        if (yxjVideoView != null)
            yxjVideoView.pausePlay();
    }


    /**
     * 销毁
     */
    public void onDestroy() {

        reset();

        if (yxjVideoView != null)
            yxjVideoView.stop();

        yxjVideoView = null;

        tinyFrameLayout = null;

        container = null;

        manager = null;
    }


    /**
     * 静音
     */
    public void closeAudio(Context context) {
//        AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
//        audioManager.setStreamMute(AudioManager.STREAM_MUSIC,true);

        if (yxjVideoView != null) {
            yxjVideoView.closeVolume();
        }
    }


    /**
     * 取消静音
     */
    public void openAudio(Context context) {
//        AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
//        audioManager.setStreamMute(AudioManager.STREAM_MUSIC,false);
//        audioManager.setMicrophoneMute(false);
        if (yxjVideoView != null) {
            yxjVideoView.openVolume();
        }
    }


    public interface CurrentWindowStateListener {
        void onChangerWindowState(int state);
    }


    private CurrentWindowStateListener currentWindowStateListener;

    public void setCurrentWindowStateListener(CurrentWindowStateListener currentWindowStateListener) {
        this.currentWindowStateListener = currentWindowStateListener;
    }


}
